/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb;

import java.awt.Image;
import javax.swing.Action;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.modules.java.api.common.ant.UpdateHelper;
import org.netbeans.spi.java.project.support.ui.PackageView;
import org.netbeans.spi.project.ActionProvider;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.netbeans.spi.project.support.ant.ReferenceHelper;
import org.netbeans.spi.project.ui.LogicalViewProvider;
import org.netbeans.spi.project.ui.support.CommonProjectActions;
import org.netbeans.spi.project.ui.support.NodeFactorySupport;
import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
import org.openide.filesystems.FileObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Node;
import org.openide.util.ChangeSupport;
import org.openide.util.ImageUtilities;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author farouka
 */
public class AWNetbeansLogicalView implements LogicalViewProvider
{

    private final AWNetbeansProject project;
    private final UpdateHelper updateHelper;
    private final PropertyEvaluator evaluator;
    private final ReferenceHelper refHelper;
    private final ChangeSupport changeSupport = new ChangeSupport(this);

    public AWNetbeansLogicalView( AWNetbeansProject project, UpdateHelper helper, PropertyEvaluator evaluator,ReferenceHelper refHelper )
    {
        this.project = project;
        assert project != null;
        this.updateHelper = helper;
        assert helper != null;
        this.evaluator = evaluator;
        assert evaluator != null;
        this.refHelper = refHelper;
        assert refHelper != null;
    }

    @Override
    public org.openide.nodes.Node createLogicalView()
    {
        return new RootNode( project );
    }

    @Override
    public Node findPath( Node root, Object target )
    {
        Project prj = root.getLookup().lookup(Project.class);
        if (prj == null) {
            return null;
        }

        if (target instanceof FileObject) {
            FileObject fo = (FileObject) target;
            Project owner = FileOwnerQuery.getOwner(fo);
            if (!prj.equals(owner)) {
                return null; // Don't waste time if project does not own the fo
            }

            for (Node n : root.getChildren().getNodes(true)) {
                Node result = PackageView.findPath(n, target);
                if (result != null) {
                    return result;
                }
            }
        }

        return null;
    }

    public ChangeSupport getChangeSupport() {
        return changeSupport;
    }

    public PropertyEvaluator getEvaluator() {
        return evaluator;
    }


    public AWNetbeansProject getProject() {
        return project;
    }

    public UpdateHelper getUpdateHelper() {
        return updateHelper;
    }

    public ReferenceHelper getRefHelper() {
        return refHelper;
    }
    

    private static final class RootNode extends AbstractNode
    {

        final AWNetbeansProject project;

        public RootNode( AWNetbeansProject project )
        {
            super( NodeFactorySupport.createCompositeChildren(
                    project, NBawConstants.REGISTERED_NODE_LOCATION ), Lookups.singleton( project ) );
            this.project = project;
            setIconBaseWithExtension( NBawConstants.PROJECT_ICON_PATH );
        }

        @Override
        public Action[] getActions( boolean arg0 )
        {
//            Action[] nodeActions = new Action[ 8 ];
//            nodeActions[0] = CommonProjectActions.newFileAction();
//            nodeActions[1] = ProjectSensitiveActions.projectCommandAction(
//                    ActionProvider.COMMAND_CLEAN, "Clean", null );
//            nodeActions[2] = ProjectSensitiveActions.projectCommandAction(
//                    ActionProvider.COMMAND_BUILD, "Launch", null );
//            nodeActions[3] = CommonProjectActions.copyProjectAction();
//            nodeActions[4] = CommonProjectActions.deleteProjectAction();
//            nodeActions[5] = CommonProjectActions.setAsMainProjectAction();
//            nodeActions[6] = CommonProjectActions.closeProjectAction();

//            return CommonProjectActions.forType("org-netbeans-modules-java-j2seproject"); // NOI18N
            return CommonProjectActions.forType("com-farent-solutions-plugins-aribaweb-AWNetbeansProject"); // NOI18N
//        @Override
//        public Action[] getActions( boolean context ) {
//
//        }

//            return CommonProjectActions.forType("org-netbeans-modules-java-j2seproject"); // NOI18N
//            return nodeActions;
        }

        @Override
        public Image getIcon( int type )
        {
            return ImageUtilities.loadImage(  NBawConstants.PROJECT_ICON_PATH );
        }

        @Override
        public Image getOpenedIcon( int type )
        {
            return getIcon( type );
        }

        @Override
        public String getDisplayName()
        {
            return project.getProjectDirectory().getName();
        }
    }
}
